package cn.cds.withyou.admin.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;

import com.squareup.otto.Subscribe;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.cds.withyou.admin.Application;
import cn.cds.withyou.admin.R;
import cn.cds.withyou.admin.actions.ActionsCreator;
import cn.cds.withyou.admin.stores.FeedbackStore;
import cn.cds.withyou.admin.ui.adapter.FeedbackAdapter;
import cn.cds.withyou.admin.ui.widget.CommonTitleBar;
import cn.cds.withyou.lib.helper.ViewTreeHelper;
import cn.ljs.android.buses.ActionBus;
import cn.ljs.android.buses.DataBus;
import cn.ljs.android.ui.BaseActivity;
import cn.ljs.android.util.TUtil;

/**
 * <p> Description: </p>
 * <p/>
 * <p> Copyright: Copyright (c) 2016 </p>
 *
 * @author: Json.Lee
 * @Version: 1.0
 * @CreateTime: 7/5/2016 23:08
 */
public class FeedbackCheckActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.common_title_bar)
    CommonTitleBar mTitleBar;
    @Bind(R.id.content_rv)
    RecyclerView mContentRv;
    @Bind(R.id.refresh_sl)
    SwipeRefreshLayout mRefreshSl;

    private ActionsCreator mCreator;
    private ActionBus mActionBus;
    private DataBus mDataBus;
    private FeedbackStore mStore;

    private FeedbackAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_request_verify);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        mTitleBar.setTitle("意见反馈");

        mCreator = ActionsCreator.get();
        mActionBus = Application.getInstance().getActionsBus();
        mDataBus = Application.getInstance().getDataBus();
        mStore = FeedbackStore.get();

        mDataBus.register(this);
        mActionBus.register(mStore);

        mRefreshSl.setOnRefreshListener(this);
        mContentRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mContentRv.setHasFixedSize(true);
        mAdapter = new FeedbackAdapter(this, mStore.getData());
        mContentRv.setAdapter(mAdapter);

        ViewTreeHelper.onGlobalLayout(mContentRv, new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                onRefresh();
            }
        });
    }

    @Subscribe
    public void updateUI(FeedbackStore.FeedbackEvent event) {
        mRefreshSl.setRefreshing(false);
        switch (event.code) {
            case FeedbackStore.FeedbackEvent.LOAD_FEEDBACK_SUCC:
                mAdapter.notifyDataSetChanged();
                break;
            case FeedbackStore.FeedbackEvent.LOAD_FEEDBACK_FAIL:
                TUtil.shortToast("加载数据失败，请重试。");
                break;
            default:
                return;
        }
    }

    @Override
    protected void onDestroy() {
        mActionBus.unregister(mStore);
        mDataBus.unregister(this);
        super.onDestroy();
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case CommonTitleBar.LEFT_BUTTON_ID:
                finish();
                break;
            default:
                return;
        }
    }

    @Override
    public void onRefresh() {
        mRefreshSl.setRefreshing(true);
        mCreator.requestAllFeedback();
    }
}
